package com.example.pt.controller;


import com.example.pt.service.InvitationService;
import com.example.pt.service.UserLevelService;
import com.example.pt.service.UserService;
import com.example.pt.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.MalformedParameterizedTypeException;
import java.sql.Date;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/register")
@CrossOrigin(origins = "*")
public class RegisterController {
    @Autowired
    private UserService userService;
    @Autowired
    private InvitationService invitationService;
    @Autowired
    private UserLevelService userLevelService;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    //密码长度应于8-17位之间。
    //注册接口
    @PostMapping("")
    public Map<String, Object> register(@RequestBody Map<String, String> registerData) {// registerData包含username,password,email,confirmPassword,invitationCode
        int uid = Integer.valueOf(registerData.get("uid"));
        String password = registerData.get("password");
        String email = registerData.get("email");
        String confirmPassword = registerData.get("confirmPassword");
        String invitationCode = registerData.get("invitationCode");
        int points = -1;
        int level = 0;
        int status = 0;
        Date registerDate = Date.valueOf(LocalDate.now());

        int usertype = 1; // 1是普通用户 0是管理员
        String headShot = "E:/PrivateTracker/HeadShot/1.jpg"; // 初始头像路径


        if (userService.searchByUid(uid) != null) { //判断用户名是否已经存在
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("status", "error");
            errorResponse.put("message", "用户名已存在！");
            return errorResponse;
        }

        if (password.equals(confirmPassword)) { //密码和确认密码相等
            String encodePassword = passwordEncoder.encode(password); //密码加密

            if (invitationCode != null && !invitationCode.isEmpty()) {
                System.out.println("invitationCode: " + invitationCode);
                if (invitationService.searchByInvitation_code(invitationCode)) {
                    System.out.println("invitationCode有效");
                    points = 101;
                    level = 1;
                } else {
                    Map<String, Object> errorResponse = new HashMap<>();
                    errorResponse.put("status", "error");
                    errorResponse.put("message", "邀请码错误！");
                    return errorResponse;
                }
            }

            String username = "普通用户";
            // 先创建用户
            userService.creatNewUser(uid, username, encodePassword, email, usertype, headShot, level, points, registerDate);
            
            // 如果有邀请码，处理邀请码关联
            if (invitationCode != null && !invitationCode.isEmpty()) {
                userLevelService.rewardNewUserWithInvitation(uid, invitationCode);
            }
            
            Map<String, Object> response = new HashMap<>();
            response.put("status", "success");
            response.put("message", "注册成功!");
            String token = JWTUtil.generateToken(usertype,uid);
            response.put("token", token);
            response.put("uid", uid);
            return response;
        } else {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("status", "error");
            errorResponse.put("message", "确认密码与密码不符，请重新输入！");
            return errorResponse;
        }
    }
}

